package com.ssy.system.controller;

import com.ssy.common.CodeMsg;
import com.ssy.common.Constant;
import com.ssy.common.utils.MD5Utils;
import com.ssy.common.utils.PageResult;
import com.ssy.common.utils.Result;
import com.ssy.system.pojo.Role;
import com.ssy.system.pojo.User;
import com.ssy.system.service.IPermissionService;
import com.ssy.system.service.IRoleService;
import com.ssy.system.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author ssy
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;

    /**
     * 角色的分页查询
     *
     * @param roleVo
     * @return
     */
    @GetMapping("/loadAllRolePage")
    public PageResult loadAllRolePage(RoleVo roleVo) {
        return this.roleService.loadAllRolePage(roleVo);
    }

    /**
     * 添加角色
     *
     * @param role
     * @return
     */
    @PostMapping("/addRole")
    public Result addRole(@RequestBody Role role) {
        role.setCreateTime(new Date());
        return this.roleService.insertRole(role);
    }

    /**
     * 修改角色
     *
     * @param role
     * @return
     */
    @PostMapping("/updateRole")
    public Result updateRole(@RequestBody Role role) {
        return this.roleService.updateRole(role);
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteRole/{id}")
    public Result deleteRole(@PathVariable Integer id) {
        return this.roleService.deleteRoleById(id);
    }

    /**
     * 批量删除角色
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/bathDeleteRole/{ids}")
    public Result bathDeleteRole(@PathVariable Integer[] ids) {
        return this.roleService.bathDeleteRole(ids);
    }

    /**
     * 修改角色状态
     *
     * @param id
     * @param state
     * @return
     */
    @PostMapping("/updateRoleState/{id}/{state}")
    public Result updateRoleState(@PathVariable Integer id, @PathVariable Integer state) {
        Role role = new Role();
        role.setId(id);
        role.setState(state);
        return this.roleService.updateRole(role);
    }

    /**
     * 角色授权
     *
     * @param roleId
     * @param pids
     * @return
     */
    @PostMapping("/saveRolePermission")
    public Result saveRolePermission(Integer roleId, Integer[] pids) {
        try {
            this.roleService.saveRolePermission(roleId, pids);
            return new Result(CodeMsg.AUTHORIZATION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(CodeMsg.AUTHORIZATION_ERROR);
        }

    }

    /**
     * 根据角色Id加载并中权限和菜单
     *
     * @param roleId 角色ID
     * @return
     */
    @PostMapping("/loadRolePermission")
    public Result loadRolePermission(@RequestParam("id") Integer roleId) {
        return this.permissionService.queryRolePermissionByRoleId(roleId);
    }

}
